import {getToken, setRefreshToken, setToken} from "@/lib/auth";
import {NextResponse} from "next/server";
import ApiProxy from "@/app/api/proxy";

const FASTAPI_WORKOUTS_URL = "http://127.0.0.1:8000/workouts/workouts"
const FASTAPI_CREATE_WORKOUT_URL = "http://127.0.0.1:8000/workouts"

export async function GET(request) {
    const authToken = await getToken();
    if (!authToken) {
        return NextResponse.json({}, {status: 401})
    }
    const response = await ApiProxy.get(FASTAPI_WORKOUTS_URL, true);
    const result = await response.json();
    return NextResponse.json(result, {status: response.status});
}

export async function POST(request) {
    const requestData = await request.json();
    const response = await ApiProxy.post(FASTAPI_CREATE_WORKOUT_URL, requestData, true);
    try {
        const responseData = await response.json();
        console.log(responseData);
    } catch (error) {
        NextResponse.json({message: "Invalid request"}, {status: response.status})
    }

    if (response.ok) {
        return NextResponse.json(responseData, {status: 200});
    }
    // 登录失败返回响应信息
    return NextResponse.json({}, {status: 400});
}